﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Security;

namespace MinistryStandardPlatformWeb
{
    public class IsLoginAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var id = httpContext.User.Identity as FormsIdentity;
            if (id != null && id.IsAuthenticated && id is FormsIdentity)
            {
                return true;
            }
            else
            {
                return base.AuthorizeCore(httpContext);
            }
        }
    }

    public class MenuAuthorizedAttribute : AuthorizeAttribute
    {
        private string menuId;
        public MenuAuthorizedAttribute(string menuId)
        {
            this.menuId = menuId;
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (menuId.Contains(';'))
            {
                string[] menus = menuId.Split(';');
                foreach (string str in menus)
                {
                    if (CommonFunctions.GetLoginUser().HasMenuRight(str))
                    {
                        return true;
                    }
                }
                return false;
            }
            else
            {
                return CommonFunctions.GetLoginUser().HasMenuRight(menuId);
            }
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            //base.HandleUnauthorizedRequest(filterContext);
            filterContext.HttpContext.Response.Write("<script>alert('您不具备该项菜单权限，如非系统故障，请勿违规操作')</script>");
            filterContext.HttpContext.Response.End();
            FormsAuthentication.SignOut();
        }
    }
}